<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of commandes
 *
 * @author Gidaff_01
 */
class Commandes extends Admin_Controller {

    //put your code here
    function Commandes() {
        parent::Admin_Controller();
        $this->load->model('Commandes_m');
        $this->load->library('pagination');
        //$this->load->library('upload');
        $this->load->library('session');
        $this->load->helper('date');

        $this->_data['erreur'] = "";
    }

    function index() {
        $this->liste_commandes();
    }

    function liste_commandes() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/commandes/liste_commandes/' . '/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Commandes_m->count_all_commandes();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['commandes'] = $this->Commandes_m->get_all_commandes($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Commandes_m->count_all_commandes();
        $this->_data['page_title'] = "Liste de commandes";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('commandes/liste_commandes');
    }

    function detail_commandes($commandes_id) {
        $query = $this->Commandes_m->get_commande_by_id($commandes_id);
        $query1 = $this->Commandes_m->get_product_by_commande_id($commandes_id);
        $query2 = $this->Commandes_m->get_transport_id($commandes_id);
        $query3 = $this->Commandes_m->get_transport();
        $this->_data['detail_commandes'] = $query;
        $this->_data['detail_product'] = $query1;
        $this->_data['id_trans'] = $query2;
        $this->_data['transport'] = $query3;
        $this->_data['page_title'] = "Commandes";
        $this->display_view('commandes/detail_commandes');
    }

    function delivered($commandes_id,$user_id) {
        is_admin();
        $this->load->library('email');
        $user_data = $this->Commandes_m->get_user_by_id($user_id);

        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';

        $this->email->initialize($config);
        if ($user_data->num_rows() > 0) {
            $user = $user_data->row();

            $this->email->from('administrator@lehu.com', 'Zoo-market');
            $this->email->to($user->email);
            $this->email->subject('Confirmer la livraison');
            $message = 'Hello ' . $user->display_name;
            $message .= '.<p> Nous avons été la livraison du produit que vous commandez sur notre site web</p>';
            $this->email->message($message);
//            echo $user->email;
//            echo $user->display_name;
//            echo $message;die;
            $this->email->send();

            $this->Commandes_m->delivered($commandes_id);
            redirect(site_url('commandes'));
        } $this->Commandes_m->delivered($commandes_id);
            redirect(site_url('commandes'));
    }

    function delete_commandes($id, $per_page, $off_set) {
        $this->Commandes_m->delete_commandes($id);
        redirect('commandes/liste_commandes' . '/' . $per_page . '/' . $off_set);
    }

    function search_commandes($livre='all') {
        is_admin();
        if ($this->input->post('submit') == TRUE) {
            $livre = $this->input->post('search_livre');
            //echo $livre;die;
            $this->_data['selected_livre'] = $livre;
            $per_page = $this->uri->segment(4);
            if ($per_page == "") {
                $per_page = 7;
            }
            $off_set = $this->uri->segment(5);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/commandes/search_commandes/' . '/' . $livre . '/' . $per_page . '/';
            $config['per_page'] = '7';
            $config['uri_segment'] = 5;
            $config['total_rows'] = $this->Commandes_m->count_commandes_by_livre($livre);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['commandes'] = $this->Commandes_m->get_commandes_by_livre($livre, $config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Commandes_m->count_commandes_by_livre($livre);
            $this->_data['page_title'] = "Liste de commandes";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('commandes/liste_commandes');
        } else {
            $this->liste_commandes();
        }
    }

}

?>
